package com.lusheng.demo02.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lusheng.demo02.entity.EduTeacher;
import com.lusheng.demo02.mapper.EduTeacherMapper;
import com.lusheng.demo02.service.EduTeacherService;
import com.lusheng.demo02.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.jsqlparser.statement.select.Limit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//定义讲师管理的 RESTful 接口（增删改查），依赖 Service 层和R工具类，
// 是对外提供 API 的核心，需在 Service 和工具类之后实现。
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
public class EduTeacherController {
    @Autowired
    public EduTeacherService eduTeacherService;

    @ApiOperation(value = "查询所有讲师")
    @GetMapping("findAll")
    public  R findAllTeacher(){
        List<EduTeacher> teacherList = eduTeacherService.list(null);
        System.out.println(teacherList);
        return R.ok().data("teacherList",teacherList);
    }

    @ApiOperation(value = "根据id，逻辑删除讲师")
    @DeleteMapping("{id}")
    public R remoteTeacherById(@ApiParam(name = "id",value = "讲师id",required = true)@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "分页查询讲师")
    @GetMapping("page/{current}/{limit}")
    public R pageListTeacher(@PathVariable Long current,@PathVariable Long limit){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        System.out.println(eduTeacher);
        return R.ok().data("teacher",eduTeacher);
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }



}
